home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_333 / multiplot / source / mplot_src / packer.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  111 lines

  1. /*----------------------------------------------------------------------*
  2.  * packer.c Convert data to "cmpByteRun1" run compression.     11/15/85
  3.  *
  4.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  5.  * This software is in the public domain.
  6.  *
  7.  *      control bytes:
  8.  *       [0..127]   : followed by n+1 bytes of data.
  9.  *       [-1..-127] : followed by byte to be repeated (-n)+1 times.
  10.  *       -128       : NOOP.
  11.  *
  12.  * This version for the Commodore-Amiga computer.
  13.  *----------------------------------------------------------------------*/
  14. #include "packer.h"
  15.  
  16. #define DUMP    0
  17. #define RUN     1
  18.  
  19. #define MinRun 3
  20. #define MaxRun 128
  21. #define MaxDat 128
  22.  
  23. LONG putSize;
  24. #define GetByte()       (*source++)
  25. #define PutByte(c)      { *dest++ = (c);   ++putSize; }
  26.  
  27. char buf[256];  /* [TBD] should be 128?  on stack?*/
  28.  
  29. BYTE *PutDump(dest, nn)  BYTE *dest;  int nn; {
  30.         int i;
  31.  
  32.         PutByte(nn-1);
  33.         for(i = 0;  i < nn;  i++)   PutByte(buf[i]);
  34.         return(dest);
  35.         }
  36.  
  37. BYTE *PutRun(dest, nn, cc)   BYTE *dest;  int nn, cc; {
  38.         PutByte(-(nn-1));
  39.         PutByte(cc);
  40.         return(dest);
  41.         }
  42.  
  43. #define OutDump(nn)   dest = PutDump(dest, nn)
  44. #define OutRun(nn,cc) dest = PutRun(dest, nn, cc)
  45.  
  46. /*----------- PackRow --------------------------------------------------*/
  47. /* Given POINTERS TO POINTERS, packs one row, updating the source and
  48.    destination pointers.  RETURNs count of packed bytes.*/
  49. LONG PackRow(pSource, pDest, rowSize)
  50.     BYTE **pSource, **pDest;   LONG rowSize; {
  51.     BYTE *source, *dest;
  52.     char c,lastc = '\0';
  53.     BOOL mode = DUMP;
  54.     short nbuf = 0;             /* number of chars in buffer */
  55.     short rstart = 0;           /* buffer index current run starts */
  56.  
  57.     source = *pSource;
  58.     dest = *pDest;
  59.     putSize = 0;
  60.     buf[0] = lastc = c = GetByte();  /* so have valid lastc */
  61.     nbuf = 1;   rowSize--;      /* since one byte eaten.*/
  62.  
  63.  
  64.     for (;  rowSize;  --rowSize) {
  65.         buf[nbuf++] = c = GetByte();
  66.         switch (mode) {
  67.                 case DUMP:
  68.                         /* If the buffer is full, write the length byte,
  69.                            then the data */
  70.                         if (nbuf>MaxDat) {
  71.                                 OutDump(nbuf-1);
  72.                                 buf[0] = c;
  73.                                 nbuf = 1;   rstart = 0;
  74.                                 break;
  75.                                 }
  76.  
  77.                         if (c == lastc) {
  78.                             if (nbuf-rstart >= MinRun) {
  79.                                 if (rstart > 0) OutDump(rstart);
  80.                                 mode = RUN;
  81.                                 }
  82.                             else if (rstart == 0)
  83.                                 mode = RUN;     /* no dump in progress,
  84.                                 so can't lose by making these 2 a run.*/
  85.                             }
  86.                         else  rstart = nbuf-1;          /* first of run */
  87.                         break;
  88.  
  89.                 case RUN: if ( (c != lastc)|| ( nbuf-rstart > MaxRun)) {
  90.                         /* output run */
  91.                         OutRun(nbuf-1-rstart,lastc);
  92.                         buf[0] = c;
  93.                         nbuf = 1; rstart = 0;
  94.                         mode = DUMP;
  95.                         }
  96.                         break;
  97.                 }
  98.  
  99.         lastc = c;
  100.         }
  101.  
  102.     switch (mode) {
  103.         case DUMP: OutDump(nbuf); break;
  104.         case RUN: OutRun(nbuf-rstart,lastc); break;
  105.         }
  106.     *pSource = source;
  107.     *pDest = dest;
  108.     return(putSize);
  109.     }
  110.  
  111.